package com.ztgf.order.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.ExcelExport2;
import com.ztgf.order.models.bo.OrderShopStatisticsBO;
import com.ztgf.order.models.bo.OrderStatisticsBO;
import com.ztgf.order.models.service.IOrderSalesStatisticsService;
import com.ztgf.order.models.vo.OrderSalesCategoryNewVO;
import com.ztgf.order.models.vo.OrderSalesCategoryShopVO;
import com.ztgf.order.models.vo.OrderSalesCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-07-22 16:04
 * Description: 订单销量统计控制器
 */
@RestController
@RequestMapping("/cms/orderSalesStatisticsController")
@Api(value = "OrderSalesStatisticsController", tags = "后台管理系统销量统计接口")
public class OrderSalesStatisticsController {

    @DubboReference
    private IOrderSalesStatisticsService orderSalesStatisticsService;

    @PostMapping(value = "/queryOrderSalesToDay")
    @ApiOperation(value = "订单今日销售总额", notes = "订单今日销售总额")
    public Result queryOrderSalesToDay(OrderStatisticsBO bo) {
        String price = orderSalesStatisticsService.queryOrderSalesToDay(bo);
        return new Result<>(price);
    }

    @PostMapping(value = "/queryOrderDealCount")
    @ApiOperation(value = "交易额统计", notes = "交易额统计")
    public Result queryOrderDealCount(OrderStatisticsBO bo) {
        String price = orderSalesStatisticsService.queryOrderDealCount(bo);
        return new Result<>(price);
    }

    @PostMapping(value = "/queryOrderSalesCategory")
    @ApiOperation(value = "各类目整体销售额统计", notes = "各类目整体销售额统计")
    public Result queryOrderSalesCategory(OrderStatisticsBO bo) {
//        Page<OrderSalesCategoryVO> page = orderSalesStatisticsService.queryOrderSalesCategory(bo);
        Page<OrderSalesCategoryNewVO> page = orderSalesStatisticsService.queryNewOrderSalesCount(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/queryOrderVariousShopItems")
    @ApiOperation(value = "各类目店铺销售额统计", notes = "各类目店铺销售额统计")
    public Result queryOrderVariousShopItems(OrderShopStatisticsBO bo) {
        Page<OrderSalesCategoryShopVO> page = orderSalesStatisticsService.queryOrderVariousShopItems(bo);
        return new Result<>(page);
    }

    @GetMapping(value = "/exportOrderSalesCategory")
    @ApiOperation(value = "各类目整体销售额统计导出", notes = "各类目整体销售额统计导出")
    public void exportOrderSalesCategory(HttpServletResponse response, String startTime, String endTime) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"编号", "类目名称", "订单销量"};
        //查询数据
        OrderStatisticsBO bo = new OrderStatisticsBO();
        bo.setPageNo(1);
        bo.setPageSize(1000);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bo.setStartTime(startTime);
            bo.setEndTime(endTime);
        }
        Page<OrderSalesCategoryNewVO> page = orderSalesStatisticsService.queryNewOrderSalesCount(bo);
        List<OrderSalesCategoryNewVO> list = page.getRecords();
        if(CustomizeUtil.listIsNull(list)){
            OrderSalesCategoryNewVO orderSalesCategoryNewVO = new OrderSalesCategoryNewVO();
            orderSalesCategoryNewVO.setCategoryName("暂无信息");
            orderSalesCategoryNewVO.setOrderPrice(new BigDecimal("0.00"));
            list.add(orderSalesCategoryNewVO);
        }
        ExcelExport2.export(response, list, attributeNames, "_各类目整体销售额");
    }

    @GetMapping(value = "/exportOrderSalesVariousShopItems")
    @ApiOperation(value = "各类目店铺销售额统计导出", notes = "各类目店铺销售额统计导出")
    public void exportOrderSalesVariousShopItems(HttpServletResponse response, String startTime, String endTime) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"编号", "类目编号", "店铺名称", "类目名称", "销售额"};
        //查询数据
        OrderShopStatisticsBO bo = new OrderShopStatisticsBO();
        bo.setPageNo(1);
        bo.setPageSize(10000);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bo.setStartTime(startTime);
            bo.setEndTime(endTime);
        }
        Page<OrderSalesCategoryShopVO> page = orderSalesStatisticsService.queryOrderVariousShopItems(bo);
        List<OrderSalesCategoryShopVO> list = page.getRecords();
        if(CustomizeUtil.listIsNull(list)){
            OrderSalesCategoryShopVO orderSalesCategoryNewVO = new OrderSalesCategoryShopVO();
            orderSalesCategoryNewVO.setCategoryName("暂无信息");
            orderSalesCategoryNewVO.setShopName("暂无信息");
            orderSalesCategoryNewVO.setOrderSales(new BigDecimal("0.00").toString());
            list.add(orderSalesCategoryNewVO);
        }
        ExcelExport2.export(response, list, attributeNames, "_各类目店铺销售额");
    }
}
